﻿Imports System.IO
Imports System.Xml

Public Class Form1
    Dim langList As New LanguageCollection

    Dim srcLang As Language
    Dim targetLang As Language

    Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        ReadLanguageCodes(Application.StartupPath)
    End Sub

    Private Sub Button2_Click(sender As Object, e As EventArgs) Handles btn_switch_text.Click
        Dim str1 As String = txt_src.Text
        Dim str2 As String = txt_trsl.Text

        txt_src.Text = str2
        txt_trsl.Text = str1

        Dim srcI As Integer = cm_lang1.SelectedIndex
        Dim targI As Integer = cm_lang2.SelectedIndex

        cm_lang1.SelectedIndex = targI
        cm_lang2.SelectedIndex = srcI
    End Sub

    Private Sub btn_switch_lang_Click(sender As Object, e As EventArgs) Handles btn_switch_lang.Click
        'Dim lang1 As Language = srcLang
        'Dim lang2 As Language = targetLang

        'srcLang = lang2
        'targetLang = lang1

        Dim srcI As Integer = cm_lang1.SelectedIndex
        Dim targI As Integer = cm_lang2.SelectedIndex

        cm_lang1.SelectedIndex = targI
        cm_lang2.SelectedIndex = srcI

        'Me.Text = String.Format("{0} - {1}", srcLang.LocaleName, targetLang.LocaleName)
    End Sub

    Private Sub ReadLanguageCodes(ByVal appPath As String)
        Dim fs As New FileStream(String.Format("{0}\availableLanguages.xml", appPath), FileMode.Open)
        Dim sr As New StreamReader(fs)

        Dim doc As New XmlDocument
        doc.LoadXml(sr.ReadToEnd)

        For Each nod As XmlNode In doc.SelectNodes("/locales/locale")
            langList.Add(New Language(nod.SelectSingleNode("localeString").InnerText, nod.SelectSingleNode("localeName").InnerText))

            cm_lang1.Items.Add(nod.SelectSingleNode("localeName").InnerText)
            cm_lang2.Items.Add(nod.SelectSingleNode("localeName").InnerText)
        Next

        cm_lang1.SelectedIndex = 0
        cm_lang2.SelectedIndex = 1

        srcLang = langList(0)
        targetLang = langList(1)

        sr.Close()
        fs.Close()
    End Sub

    Private Sub TranslateLanguage(ByVal srcText As String)
        Dim targetUri As String = String.Format("https: //translate.yandex.net/api/v1.5/tr/translate?key=trnsl.1.1.20150902T111618Z.54a5e446868da615.f213f2dc16a4c97ef1e7e6253ddd1a236e3e540b&lang={0}-{1}&text={2}", srcLang.Locale, targetLang.Locale, srcText)

        Dim doc As New XmlDocument
        doc.Load(targetUri)

        txt_trsl.Text = doc.SelectSingleNode("Translation").SelectSingleNode("text").InnerText
    End Sub

    Private Sub DetectSourceLanguage(ByVal srcText As String)
        Dim targetUri As String = String.Format("https://translate.yandex.net/api/v1.5/tr/detect?key=trnsl.1.1.20150902T111618Z.54a5e446868da615.f213f2dc16a4c97ef1e7e6253ddd1a236e3e540b&text={0}", srcText)

        Dim doc As New XmlDocument
        doc.Load(targetUri)

        Dim targetLng As String = doc.SelectSingleNode("DetectedLang").Attributes(1).InnerText

        For i As Integer = 0 To langList.Count - 1
            If langList(i).Locale = targetLng Then
                cm_lang1.SelectedIndex = i
            End If
        Next
    End Sub

    Private Sub DetectTargetLanguage(ByVal srcText As String)
        Dim targetUri As String = String.Format("https://translate.yandex.net/api/v1.5/tr/detect?key=trnsl.1.1.20150902T111618Z.54a5e446868da615.f213f2dc16a4c97ef1e7e6253ddd1a236e3e540b&text={0}", srcText)

        Dim doc As New XmlDocument
        doc.Load(targetUri)

        Dim targetLng As String = doc.SelectSingleNode("DetectedLang").Attributes(1).InnerText

        For i As Integer = 0 To langList.Count - 1
            If langList(i).Locale = targetLng Then
                cm_lang2.SelectedIndex = i
            End If
        Next
    End Sub

    Private Sub cm_lang1_SelectedIndexChanged(sender As Object, e As EventArgs) Handles cm_lang1.SelectedIndexChanged
        srcLang = langList(cm_lang1.SelectedIndex)
    End Sub

    Private Sub cm_lang2_SelectedIndexChanged(sender As Object, e As EventArgs) Handles cm_lang2.SelectedIndexChanged
        targetLang = langList(cm_lang2.SelectedIndex)
    End Sub

    Private Sub btn_translate_Click(sender As Object, e As EventArgs) Handles btn_translate.Click
        TranslateLanguage(txt_src.Text)
    End Sub

    Private Sub Panel1_Paint(sender As Object, e As PaintEventArgs) Handles Panel1.Paint
        Dim g As Graphics = e.Graphics

        g.SmoothingMode = Drawing2D.SmoothingMode.AntiAlias
        g.InterpolationMode = Drawing2D.InterpolationMode.Bicubic

        Dim rct As Rectangle = Panel1.ClientRectangle

        Dim p1 As New Point(rct.X, rct.Y + rct.Height)
        Dim p2 As New Point(rct.X + rct.Width, rct.Y + rct.Height)

        Dim p As New Pen(Color.DimGray, 2)

        g.DrawLine(p, p1, p2)

    End Sub

    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles btn_detect.Click
        DetectSourceLanguage(txt_src.Text)
    End Sub

    'Private Sub btn_detect2_Click(sender As Object, e As EventArgs) Handles btn_detect2.Click
    '    DetectTargetLanguage(txt_trsl.Text)
    'End Sub
End Class

Public Class LanguageCollection
    Inherits Collections.Generic.List(Of Language)
End Class

Public Class Language
    Dim _locale As String
    Public ReadOnly Property Locale As String
        Get
            Return _locale
        End Get
    End Property

    Dim _localeString As String
    Public ReadOnly Property LocaleName As String
        Get
            Return _localeString
        End Get
    End Property

    Public Sub New(ByVal locale As String, ByVal localestring As String)
        _locale = locale
        _localeString = localestring
    End Sub
End Class
